package cn.fch.gotrip.controller;

import cn.fch.gotrip.api.BeanResponse;
import cn.fch.gotrip.entity.Travel;
import cn.fch.gotrip.service.TravelService;
import cn.fch.gotrip.util.UploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @program: gotrip
 * @description:
 * @CreatedBy: fch
 * @create: 2023-01-16 00:08
 **/
@Slf4j
@RestController
@RequestMapping("travel")
public class TravelController {

    @Autowired
    TravelService travelService;

    @Autowired
    UploadUtil uploadUtil;

    @PostMapping
    public BeanResponse addTravel(@RequestBody Travel travel) {
        if (travelService.addTravel(travel)) {
            return BeanResponse.ok(travel, "增加成功");
        } else {
            return BeanResponse.badRequest("增加失败");
        }
    }

    @DeleteMapping("{id}")
    public BeanResponse deleteTravel(@PathVariable Integer id) {
        if (travelService.deleteTravel(id)) {
            return BeanResponse.ok(null, "删除成功");
        } else {
            return BeanResponse.badRequest("删除失败");
        }
    }

    @PutMapping
    public BeanResponse updateTravel(@RequestBody Travel travel) {
        if (travelService.updateTravel(travel)) {
            return BeanResponse.ok(travel, "更新成功");
        } else {
            return BeanResponse.badRequest("更新失败");
        }
    }

    @PostMapping("{id}/img")
    public BeanResponse updateImg(@RequestParam MultipartFile file,
                                  @PathVariable Integer id,
                                  HttpServletRequest request) {
        log.info("文件名:{}", file.getOriginalFilename());
        log.info("地址:{}", request.getServletContext().getRealPath(""));
        log.info("id: {}", id);
        try {
            return BeanResponse.ok(travelService.updateImg(String.valueOf(id), file, request), "更新成功");
        } catch (IOException e) {
            e.printStackTrace();
            return BeanResponse.badRequest("更新失败");
        }
    }

    @GetMapping("page/{pageNum}")
    public BeanResponse queryAllTravels(@PathVariable Integer pageNum,
                                        @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        return BeanResponse.ok(travelService.queryAllTravel(pageNum, pageSize));
    }

    @GetMapping("theme/{themeId}/page/{pageNum}")
    public BeanResponse queryAllTravelsByTheme(@PathVariable Integer themeId,
                                               @PathVariable Integer pageNum,
                                               @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        return BeanResponse.ok(travelService.queryAllTravelByThemeId(themeId, pageNum, pageSize));
    }

    @GetMapping("theme")
    public BeanResponse queryAllTheme() {
        return BeanResponse.ok(travelService.queryAllTheme());
    }

    @GetMapping("activity")
    public BeanResponse queryAllActivity() {
        return BeanResponse.ok(travelService.queryAllActivity());
    }

    @GetMapping("way")
    public BeanResponse queryAllWay() {
        return BeanResponse.ok(travelService.queryAllWay());
    }
}
